//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.ServiceModel;
using System.Reflection;
using System.ServiceModel.Description;
using FinPlusWCFService;
using FinPlusCommon;
using u = FinPlusUtility.Utility;
using log = Logger.Logger;

namespace FinPlusWCFHost
{
    class Program
    {    

        static void Main(string[] args)
        {
            u.Env env;
            var p = u.GetConfigPath(Assembly.GetExecutingAssembly());
            var config = u.ConfigXml(p, out env);
            log.Initialise(p, env.ToString());

            try
            {
                var conn = config["connections"];

                using (ServiceHost host = new ServiceHost(typeof(FinPlusServices), new Uri(conn["baseaddress"])))
                {
                    var smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
                    if (smb == null)  host.Description.Behaviors.Add(new ServiceMetadataBehavior());
                    
                    host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
                    //host.AddServiceEndpoint(typeof(IFunctionService), new NetTcpBinding(), conn["endaddress"]);
                   
                    var binding = new NetTcpBinding()
                    {
                        ReceiveTimeout = TimeSpan.FromHours(double.Parse(conn["receivetimeout"].ToString())),
                        OpenTimeout = TimeSpan.FromHours(double.Parse(conn["opentimeout"])),
                        CloseTimeout = TimeSpan.FromHours(double.Parse(conn["closetimeout"])),
                        SendTimeout = TimeSpan.FromHours(double.Parse(conn["sendtimeout"]))
                    };

                    host.AddServiceEndpoint(typeof(IFunctionService), binding, conn["endaddress"]);
                  
                    var debug = host.Description.Behaviors.Find<ServiceDebugBehavior>();
                    if (debug == null)
                        host.Description.Behaviors.Add(new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true });
                    else if (!debug.IncludeExceptionDetailInFaults)
                        debug.IncludeExceptionDetailInFaults = true;

                    var throttle = new ServiceThrottlingBehavior() 
                    {   
                        MaxConcurrentCalls = int.Parse(conn["maxconcurrentcalls"]),
                        MaxConcurrentSessions = int.Parse(conn["maxconcurrentsessions"]), 
                        MaxConcurrentInstances = int.Parse(conn["maxconcurrentinstances"]) 
                    };

                    host.Description.Behaviors.Add(throttle);
                    host.CloseTimeout = TimeSpan.FromHours(double.Parse(conn["closetimeout"]));
                    host.OpenTimeout = TimeSpan.FromHours(double.Parse(conn["opentimeout"]));
                    host.Open();

                    Console.WriteLine("Press <Enter> to stop the service.");
                    Console.ReadLine();
                    host.Close();
                }
            }
            catch (ViewLoadException e)
            {
                log.Log(Logger.Level.Error, e.Message, e);
            }
        }
    }
}


